package cn.hjh.service;

import java.util.Iterator;
import java.util.List;
import java.util.Map;


import cn.hjh.dao.EmpDao;
import cn.hjh.pojo.Emp;
import cn.hjh.pojo.EmpCondition;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;


public class EmpBiz {
	private EmpDao dao = new EmpDao();
	
	public List<Emp> findTopSalary(int topSize) {
		Transaction tx = null;
		List<Emp> result = null;
		try {
			tx = dao.currentSession().beginTransaction();
			
			result = dao.findTopSalary(topSize);
			
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		}
		return result;
	}
	
	public List<Emp> findByPage(int pageNo, int pageSize) {
		Transaction tx = null;
		List<Emp> result = null;
		try {
			tx = dao.currentSession().beginTransaction();
			
			result = dao.findByPage(pageNo, pageSize);
			
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		}
		return result;
	}
	
	public List<Emp> findByConditions(Map<String, Object> conditions) {
		Transaction tx = null;
		List<Emp> result = null;
		try {
			tx = dao.currentSession().beginTransaction();
			
			StringBuilder hql = new StringBuilder("from Emp where 1=1");
			if (conditions.get("jobname") != null 
					&& ((String) conditions.get("jobname")).length() > 0)
				hql.append(" and job = :jobname");
			if (conditions.get("salary") != null 
					&& ((Double) conditions.get("salary")) != 0)
				hql.append(" and salary > :salary");
			if (conditions.get("from") != null)
				hql.append(" and hireDate > :from");
			if (conditions.get("to") != null)
				hql.append(" and hireDate < :to");
			result = dao.findByConditions(hql.toString(), conditions);
			
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		}
		return result;
	}
	
	public List<Emp> findByConditions(EmpCondition conditions) {
		Transaction tx = null;
		List<Emp> result = null;
		try {
			tx = dao.currentSession().beginTransaction();
			//HQL根据条件动态生成
			StringBuilder hql = new StringBuilder("from Emp where 1=1");
			if (conditions.getJob() != null && conditions.getJob().length() > 0)
				hql.append(" and job = :job");
			if (conditions.getSalary() != null && conditions.getSalary() != 0)
				hql.append(" and salary > :salary");
			if (conditions.getFrom() != null)
				hql.append(" and hireDate > :from");
			if (conditions.getTo() != null)
				hql.append(" and hireDate < :to");
			result = dao.findByConditions(hql.toString(), conditions);
			
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		}
		return result;
	}
	
	public List<Emp> findByConditions(Emp conditions) {
		Transaction tx = null;
		List<Emp> result = null;
		try {
			tx = dao.currentSession().beginTransaction();
			
			result = dao.findByConditions(conditions);
			
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		}
		return result;
	}
	
	public List<Emp> findByConditions(Object[] conditions) {
		Transaction tx = null;
		List<Emp> result = null;
		try {
			tx = dao.currentSession().beginTransaction();
			
			result = dao.findByConditions(conditions);
			
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		}
		return result;
	}
	
	public Emp getEmpCount_2() {
		Transaction tx = null;
		Emp count = null;
		try {
			tx = dao.currentSession().beginTransaction();
			
			count = dao.countEmp_2();
			
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		}
		return count;
	}
	
	public Long getEmpCount() {
		Transaction tx = null;
		Long count = null;
		try {
			tx = dao.currentSession().beginTransaction();
			
			count = dao.countEmp();
			
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		}
		return count;
	}

	public Long getcountsalary(Double sal) {
		Transaction tx = null;
		Long count = null;
		try {
			tx = dao.currentSession().beginTransaction();
			count = dao.ob(sal);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		}
		return count;
	}
	
	public Iterator<Emp> findAllEmp_iterate() {
		Transaction tx = null;
		Iterator<Emp> result = null;
		try {
			tx = dao.currentSession().beginTransaction();
			
			result = dao.findAllEmp_iterate();
			Emp e = null;
			while(result.hasNext()) {
				e = result.next();
				System.out.println(e.getEmpNo() + "\t" + e.getEmpName());
			}
			
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		}
		return result;
	}
	
	public List<Emp> findAllEmp() {
		Transaction tx = null;
		List<Emp> result = null;
		try {
			tx = dao.currentSession().beginTransaction();	//开始事物
			
			result = dao.findAllEmp();	//持久化操作
			
			tx.commit();	//提交事物
		} catch (HibernateException e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		}
		return result;
	}
}
